package com.zjcode.cdcer.service.impl;

import com.zjcode.cdcer.mapper.SysOrgMapper;
import com.zjcode.cdcer.mapper.SysUserMapper;
import com.zjcode.cdcer.model.SysOrg;
import com.zjcode.cdcer.model.SysUser;
import com.zjcode.cdcer.model.TkmSysteminitImportInfo;
import com.zjcode.cdcer.service.SysUserService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName:SysUserServiceImpl
 * @功能：
 * @author：wlei
 * @日期：2022/7/22-03:59
 **/
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysOrgMapper sysOrgMapper;


    @Override
    public SysUser findById(String id) {
        return sysUserMapper.selectByPrimaryKey(Long.parseLong(id));
    }

    @Override
    public String findUserExtFlag(String id) {
        // 获取用户信息
        SysUser sysUser = findById(id);
        // 机构编号
        Long orgId = sysUser.getOrgId();
        List<SysOrg> orgs = sysOrgMapper.findByParentId(orgId);
        SysOrg org = sysOrgMapper.selectByPrimaryKey(orgId);
        // 终端用户,终端用户负责数据采集
        if (orgs.size() == 0) {
            // 获取当前机构的详情
            String ancestors = org.getAncestors();
            List<String> list = new ArrayList<>();
            if (!StringUtils.isEmpty(ancestors)) {
                list.addAll(Arrays.asList(ancestors.split(",")));
            }
            list.add(id);

            String extFlag = list
                .stream()
                .collect(Collectors.joining("|"));

            return extFlag;
        } else {
            String ancestors = org.getAncestors();
            List<String> list = new ArrayList<>();
            if (!StringUtils.isEmpty(ancestors)) {
                list.addAll(Arrays.asList(ancestors.split(",")));
            }
            // 加上自己的id
            list.add(orgId.toString());

            String extFlag = list
                .stream()
                .collect(Collectors.joining("|"));

            return extFlag;
        }


    }

    @Override
    public SysOrg selectOrgInfoByName(String institutionName) {
        return sysOrgMapper.selectOrgInfoByName(institutionName);
    }

    @Override
    public String getUserExtFlag(TkmSysteminitImportInfo info, SysOrg org ) {
        Long orgId = org.getId();
        List<SysOrg> orgs = sysOrgMapper.findByParentId(orgId);
        String regionAncestors = org.getRegionAncestors();
        if (ObjectUtils.isEmpty(regionAncestors)) {
            info.setProvinceId(org.getProvinceCode());
            info.setCityId(org.getCityCode());
            info.setDistrictId(org.getRegionCode());
        }else{
            List<String> regionAncestorList = Arrays.asList(regionAncestors.split(","));
            // 查出省、市、区县的id值
            if (regionAncestorList.size()>0) {
                info.setProvinceId(regionAncestorList.get(0));
                if (regionAncestorList.size()>1) {
                    info.setCityId(regionAncestorList.get(1));
                    if (regionAncestorList.size()>2) {
                        info.setDistrictId(regionAncestorList.get(2));
                    }
                }
            }
        }
        // 终端用户,终端用户负责数据采集 ->查extFlag
        if (orgs.size() == 0) {
            // 获取当前机构的详情
            String ancestors = org.getAncestors();
            List<String> list = new ArrayList<>();
            if (!StringUtils.isEmpty(ancestors)) {
                list.addAll(Arrays.asList(ancestors.split(",")));
            }
            list.add(info.getCreateBy());

            String extFlag = list
                    .stream()
                    .collect(Collectors.joining("|"));

            return extFlag;
        } else {
            String ancestors = org.getAncestors();
            List<String> list = new ArrayList<>();
            if (!StringUtils.isEmpty(ancestors)) {
                list.addAll(Arrays.asList(ancestors.split(",")));
            }
            // 加上自己的id
            list.add(orgId.toString());

            String extFlag = list
                    .stream()
                    .collect(Collectors.joining("|"));

            return extFlag;
        }
    }

    private void findOrgs(Long orgId, List<SysOrg> sysOrgs) {
        SysOrg sysOrg = sysOrgMapper.selectByPrimaryKey(orgId);
        sysOrgs.add(sysOrg);
        if (sysOrg.getParentId() != 0) {
            findOrgs(sysOrg.getParentId(), sysOrgs);
        }
    }
}
